/**
 * @resource.cpp
 * This file is part of FEGRF.
 * 
 * FEGRF is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * See LICENSE file in main folder for more information.
 */

#include <wx/wxprec.h>

#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif

#include <wx/mstream.h>

#include "resource.h"

#if defined( __WXMSW__ ) && defined( _DEBUG )
#include <wx/msw/msvcrt.h>
#endif

wxImage CreateImageFromPngResource(const wxString& t_name)
{
	wxImage r_image;
	char *a_data = NULL;
	DWORD a_dataSize = NULL;

	if (LoadDataFromResource(a_data, a_dataSize, t_name)) {
		r_image = GetImageFromMemory(a_data, a_dataSize, wxBITMAP_TYPE_PNG);
	}

	return r_image;
}

bool LoadDataFromResource(char*& t_data, DWORD& t_dataSize, const wxString& t_name)
{
	bool r_result = false;
	HGLOBAL a_resHandle = NULL;
	HRSRC a_resource;

	a_resource = FindResource(0, t_name.wchar_str(), RT_RCDATA);

	if (0 != a_resource) {
		a_resHandle = LoadResource(NULL, a_resource);
		if (0 != a_resHandle) {
			t_data = (char*)LockResource(a_resHandle);
			t_dataSize = SizeofResource(NULL, a_resource);
			r_result = true;
		}
	}
	
	return r_result;
}

wxImage GetImageFromMemory(const char* t_data, const DWORD t_size, wxBitmapType type)
{
	wxMemoryInputStream a_is(t_data, t_size);
	return wxImage(a_is, type, -1);
}

ToolbarImage::ToolbarImage()
{
	png_new      = CreateImageFromPngResource(_("PNG_NEW")).Rescale(24, 24, wxIMAGE_QUALITY_BICUBIC);
	png_open     = CreateImageFromPngResource(_("PNG_OPEN")).Rescale(24, 24, wxIMAGE_QUALITY_BICUBIC);
	png_close    = CreateImageFromPngResource(_("PNG_CLOSE")).Rescale(24, 24, wxIMAGE_QUALITY_BICUBIC);
	png_view     = CreateImageFromPngResource(_("PNG_VIEW")).Rescale(24, 24, wxIMAGE_QUALITY_BICUBIC);
	png_repack   = CreateImageFromPngResource(_("PNG_REPACK")).Rescale(24, 24, wxIMAGE_QUALITY_BICUBIC);
	png_insert   = CreateImageFromPngResource(_("PNG_INSERT")).Rescale(24, 24, wxIMAGE_QUALITY_BICUBIC);
	png_extract  = CreateImageFromPngResource(_("PNG_EXTRACT")).Rescale(24, 24, wxIMAGE_QUALITY_BICUBIC);
	png_delete   = CreateImageFromPngResource(_("PNG_DELETE")).Rescale(24, 24, wxIMAGE_QUALITY_BICUBIC);
	png_setting  = CreateImageFromPngResource(_("PNG_SETTING")).Rescale(24, 24, wxIMAGE_QUALITY_BICUBIC);
}